package com.zorn.core.spring.mvc.support;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.context.ContextLoader;

import com.alibaba.fastjson.JSONObject;

public abstract class ServiceSupport implements IServiceSupport {

	private Map<String, Object> result = new HashMap<String, Object>();

	/* Get Bean
	================================================== */
	@SuppressWarnings("unchecked")
	public Object getBean(Class cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public Object getBean(String cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public boolean isPaging() {
		return true;
	}

	/* Get Result
	================================================== */
	@Override
	public Map<String, Object> getResult() {
		return result;
	}

	/* Set Result
	================================================== */
	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public void setResult(boolean success, String message) {
		setResult(success, "message", message);
	}

	public void setResult(boolean success, String key, Object value) {
		result = new HashMap<String, Object>(2);
		result.put("success", success);
		result.put(key, value);
	}

	public void setResult(boolean success, Object... contents) {
		result = new HashMap<String, Object>(contents.length / 2);
		for (int i = 0; i < contents.length; i++) {
			result.put(contents[i++].toString(), contents[i++]);
		}
	}

	/* Set Query Result
	================================================== */
	public <T> T setQueryResult(T data, int total) {
		return setQueryResult(true, data, total);
	}

	public <T> T setQueryResult(boolean success, T data, int total) {
		result = new JSONObject();
		result.put("success", success);
		result.put("data", data);
		result.put("total", total);
		return data;
	}

	/* Set Count Result
	================================================== */
	public int setCountResult(int count) {
		return setCountResult(true, count);
	}

	public int setCountResult(boolean success, int count) {
		result = new JSONObject();
		result.put("success", success);
		result.put("count", count);
		return count;
	}

	/* Set Process Result
	================================================== */
	public int setProcessResult(int affected, String message) {
		return setProcessResult(true, affected, message);
	}

	public int setProcessResult(boolean success, int affected, String message) {
		result = new JSONObject();
		result.put("success", success);
		result.put("affected", affected);
		result.put("message", message);
		return affected;
	}

	/* Set Insert Result
	================================================== */
	public int setInsertResult(int affected) {
		String message = null;
		if (affected == 0) {
			message = "新建失败，已成功执行操作，但新建数据没有成功。";
		} else if (affected == 1) {
			message = "已成功新建 1 条数据。";
		} else if (affected > 1) {
			message = "已成功新建 " + affected + " 条数据。";
		} else {
			message = String.format("新建失败，数据库返回代码异常，返回代码：%d。", affected);
		}
		return setProcessResult(affected > 0, affected, message);
	}

	public int setInsertResult(int affected, int id) {
		setInsertResult(affected);
		return id;
	}

	public int setInsertResult(int affected, String message) {
		return setProcessResult(true, affected, message);
	}

	public int setInsertResult(boolean success, int affected, String message) {
		return setProcessResult(success, affected, message);
	}

	/* Set Update Result
	================================================== */
	public int setUpdateResult(int affected) {
		String message = null;
		if (affected == 0) {
			message = "更新失败，已成功执行操作，但没有数据被更新。";
		} else if (affected == 1) {
			message = "已成功更新 1 条数据。";
		} else if (affected > 1) {
			message = "已成功更新 " + affected + " 条数据。";
		} else {
			message = String.format("更新失败，数据库返回代码异常，返回代码：%d。", affected);
		}
		return setProcessResult(affected > 0, affected, message);
	}

	public int setUpdateResult(int affected, String message) {
		return setProcessResult(true, affected, message);
	}

	public int setUpdateResult(boolean success, int affected, String message) {
		return setProcessResult(success, affected, message);
	}

	/* Set Delete Result
	================================================== */
	public int setDeleteResult(int affected) {
		String message = null;
		if (affected == 0) {
			message = "删除失败，已成功执行操作，但没有数据被删除。";
		} else if (affected == 1) {
			message = "已成功删除 1 条数据。";
		} else if (affected > 1) {
			message = "已成功删除 " + affected + " 条数据。";
		} else {
			message = String.format("删除失败，数据库返回代码异常，返回代码：%d。", affected);
		}
		return setProcessResult(affected > 0, affected, message);
	}

	public int setDeleteResult(int affected, String message) {
		return setProcessResult(true, affected, message);
	}

	public int setDeleteResult(boolean success, int affected, String message) {
		return setProcessResult(success, affected, message);
	}
}
